<?php
class ModelNewsCategory extends Model {
	public function getCategory($category_id) {
		$sql  = "SELECT * FROM " . DB_PREFIX . "news_category nc";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_category_description ncd ON (nc.category_id = ncd.category_id)";
			
		$sql .= " WHERE ncd.language_id ='" . (int)$this->config->get('config_language_id') . "'";
			
		$sql .= " AND nc.category_id = '" . (int)$category_id . "'";
			
		$query = $this->db->query($sql);
			
		return $query->row;
	}

	public function getCategories($data = array()) {
		$cache = md5(http_build_query($data));
		$key = 'news_category.' . $cache . (int)$this->city->getCityId();

		$news_data = $this->cache->get($key);

		if (!$news_data) {
			$sql  = "SELECT * FROM " . DB_PREFIX . "news_category nc";
			$sql .= " LEFT JOIN " . DB_PREFIX . "news_category_description ncd ON (nc.category_id = ncd.category_id)";
			$sql .= " LEFT JOIN " . DB_PREFIX . "news_type nt ON (nc.type_id = nt.type_id)";				
			$sql .= " WHERE ncd.language_id ='" . (int)$this->config->get('config_language_id') . "' AND nc.status = '1'";
				
			if (isset($data['filter_type_id']) && !is_null($data['filter_type_id'])) {
				$sql .= " AND nt.type_id = " . (int)$data['filter_type_id'];
			}
				
			$sort_data = array('ncd.title', 'na.date_added', 'na.status', 'na.sort_order');
				
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];
			} else {
				$sql .= " ORDER BY nc.date_added";
			}
				
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
				
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}
					
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}

			$query = $this->db->query($sql);
				
			$news_data = $query->rows;
				
			$this->cache->set($key, $news_data);
		}

		return $news_data;
	}
}
?>